package yf.mall.controller.api;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.entity.*;
import yf.mall.enums.AccountTypeEnum;
import yf.mall.enums.LotteryActStatusEnum;
import yf.mall.enums.LotteryGoodsStatusEnum;
import yf.mall.service.*;
import yf.mall.vo.LotteryActVo;

import java.util.List;

@RequestMapping("/api/lottery")
@RestController
public class ApiLotteryController {

    @Autowired
    private LotteryActService lotteryActService;

    @Autowired
    private LotteryGoodsService lotteryGoodsService;

    @Autowired
    private LotteryRecordService lotteryRecordService;

    @Autowired
    private LotteryCountService lotteryCountService;

    /**
     * 查处所有开启的抽奖活动取第一个
     *
     * @param userID
     * @return
     */
    @RequestMapping("act")
    public R lotteryAct(@LoginUserId Long userID) {
        List<LotteryActVo> lotteryActVos = this.lotteryActService.findByStatus(LotteryActStatusEnum.OPEN.getIndex());
        LotteryActVo lotteryActVo = null;
        if (lotteryActVos.equals( null)&&lotteryActVos ==null&&lotteryActVos.size()<=0) {
            throw new RRException("暂时没有开启抽奖活动");
        }
        lotteryActVo = lotteryActVos.get(0);
        LotteryCountEntity lotteryCountEntity = this.lotteryCountService.findByActIdAndUserId(lotteryActVo.getLotteryAct().getId(), userID);
        int count = 0;
        if (lotteryCountEntity!=null&&lotteryCountEntity.getAmount()!=null){
            count=lotteryCountEntity.getAmount();
        }
        return R.ok(lotteryActVo.getLotteryAct()).putExtra("count",count);
    }

    @RequestMapping("add")
    public R addLotteryCount(@LoginUserId long userId, Long actId) {

        LotteryCountEntity count = this.lotteryCountService.addCount(userId,actId);
        return R.ok(count);
    }


    @RequestMapping("start")
    public R startLottery(@LoginUserId Long userId, Long actId) {
        LotteryGoodsEntity lotteryGoodsEntity = this.lotteryActService.startLottery(userId, actId);
        return R.ok(lotteryGoodsEntity);
    }

    @RequestMapping("recordList")
    public R startLottery(@LoginUserId Long userId, int page, int limit) {
        List<LotteryRecordEntity> lotteryRecordEntities = this.lotteryRecordService.findByUserId(userId, page, limit);
        return R.ok(lotteryRecordEntities);
    }


}
